package com.lenovo.android.syncacc.activities;

import android.accounts.Account;
import android.accounts.AccountAuthenticatorActivity;
import android.accounts.AccountManager;
import android.content.ContentValues;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.lenovo.android.syncacc.R;
import com.lenovo.android.syncacc.controller.MyAccountScreenController;


public class MyLoginScreen extends AccountAuthenticatorActivity {

	String TAG = "MyLoginScreen";
	
	Button bt_login;
	Button bt_cancel;
	EditText usrText;
	MyAccountScreenController controller;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lenovo_account);
		controller = new MyAccountScreenController(this);
		
		bt_login = (Button)this.findViewById(R.id.login_button);
		bt_cancel = (Button)this.findViewById(R.id.cancel_button);
		usrText = (EditText)this.findViewById(R.id.username);
		
		bt_login.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				saveAndCheck();
				
			
			}
		});
	}
	
	private void saveAndCheck() {
		controller.saveAndCheck();
	}
	
	public void checkSucceeded(){
		Log.d(TAG, "********************checkSucceeded,start add Account ");
		AccountManager am = AccountManager.get(this);
		
		// The account label must be the username
		String accountLabel = usrText.getText().toString();

		// Set the account type
		String accountType = this.getString(R.string.account_type);
		
		Account account = new Account(accountLabel, accountType);
		
		if (am.addAccountExplicitly(account, null, null)) {

			Bundle result = new Bundle();
			result.putString(AccountManager.KEY_ACCOUNT_NAME, accountLabel);
			result.putString(AccountManager.KEY_ACCOUNT_TYPE, accountType);
			((AccountAuthenticatorActivity) this)
					.setAccountAuthenticatorResult(result);
			
			Log.d(TAG, "********************addAccountExplicitly succfully! ");
		}else{
			Log.d(TAG, "********************addAccountExplicitly failed! ");
		}
		
		this.finish();
		
	}

}
